import {Component} from '@angular/core';
import {App, IonicPage, NavController} from 'ionic-angular';
import {SERIES_PROCEDURE_URL, YIELD_COMPARISO_URL} from "../../providers/Constants";
import {HttpService} from "../../providers/HttpService";
import {PublicService} from "../../common/public.service";
import {DatePicker} from "@ionic-native/date-picker";

/**
 *
 */

@IonicPage()
@Component({
  selector: 'page-work-check',
  templateUrl: 'work-check.html',
})
export class WorkCheckPage {

  bzDate: string;//班组日期
  bzDate_old: string;//监控日期是否变化
  bzSelect: string;//班组选择
  procedureId: string;//工序
  seriesCode: string = "";//产品系列

  //扫描后把数据保存在这里
  public dataList: Array<Object> = [];

  //产品系列
  public seriesList: Array<Object> = [];

  constructor(public navCtrl: NavController,
              public http: HttpService,
              public app: App,
              public datePicker: DatePicker,
              public pub: PublicService) {
  }

  ionViewDidLoad() {
    this.bzDate = sessionStorage.getItem("setWorkTime");
    if (!this.bzDate) {
      this.bzDate = this.pub.getDate(new Date(), 0);
    }
    this.bzSelect = sessionStorage.getItem("setBzSelect");
    this.procedureId = sessionStorage.getItem("workProcedure");
    this.bzDate_old = this.bzDate;
    this.getSeriesByProcedure();
  }

  ionViewWillEnter() {
    this.initList();
  }

  /**
   * 查询日期
   */
  getDate() {
    this.datePicker.show({
      titleText: "",
      date: this.bzDate,
      mode: 'date',
      maxDate: new Date(),
      is24Hour: true,
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_HOLO_LIGHT
    }).then(
      date => {
        this.bzDate = this.pub.getDate(date, 0);
        if (this.bzDate != this.bzDate_old) {
          this.bzDate_old = this.bzDate;
          this.changeQuery();
        }
      },
      err => {
        console.log('Error occurred while getting date: ', err);
      }
    );
  }

  /**
   * 根据工序查询产品系列
   */
  getSeriesByProcedure() {
    let params = {sessionId: sessionStorage.getItem("session"), procedureId: this.procedureId};
    this.http.get(SERVER_URL + SERIES_PROCEDURE_URL, '&', params, false).then(res => {
      if (res.success) {
        this.seriesList = res.data.seriesList;
      } else {
        this.pub.presentLoadingToast(res.msg);
        if (res.timeout) {
          setTimeout(() => {
            this.app.getRootNav().setRoot('LoginPage');
          }, 1500);
        }
      }
    });
  }

  /**
   *列表
   */
  initList() {
    this.dataList = [];
    let params = {
      sessionId: sessionStorage.getItem("session"),
      fdDate: this.bzDate,
      fdGroupType: this.bzSelect,
      fdProcedureId: this.procedureId,
      seriesCode: this.seriesCode
    };
    this.http.get(SERVER_URL + YIELD_COMPARISO_URL, '&', params, false).then(res => {
      if (res.success) {
        this.dataList = res.data.rcyList;
      } else {
        this.pub.presentLoadingToast(res.msg);
        if (res.timeout) {
          setTimeout(() => {
            this.app.getRootNav().setRoot('LoginPage');
          }, 1500);
        }
      }
    });
  }


  changeQuery() {
    this.initList();
  }
}
